<?php
/*
 *   @author  ykm
 *   @date  2015.05.12
 *   @description  pdo 操作数据库表 单例化类
 */
class DPDO{
	private $DSN;
	private $DBUser;
	private $DBPwd;
	private $longLink;
	private $pdo;
	
	private function __construct($dsn, $DBUser, $DBPwd, $longLink = false) {
		$this->DSN = $dsn;
		$this->DBUser = $DBUser;
		$this->DBPwd = $DBPwd;
		$this->longLink = $longLink;
		$this->connect();
	}
	
	private function __clone(){}
	
	static public function instance($dsn, $DBUser, $DBPwd, $longLink = false){
		static $singleton = array();
		$singIndex = md5($dsn . $DBUser . $DBPwd . $longLink);
		if (empty($singleton[$singIndex])) {
			$singleton[$singIndex] = new self($dsn, $DBUser, $DBPwd, $longLink = false);
		}
		return $singleton[$singIndex]->pdo;
	}
	
	private function connect(){
		try{
			if($this->longLink){
				$this->pdo = new PDO($this->DSN, $this->DBUser, $this->DBPwd, array(PDO::ATTR_PERSISTENT => true));
			}else{
				$this->pdo = new PDO($this->DSN, $this->DBUser, $this->DBPwd);
			}
			$this->pdo->query('SET NAMES UTF-8');
		} catch(PDOException $e) {
			die('Error:' . $e->getMessage() . '<br/>');
		}
	}
}
?>